﻿package com.saint.bitmapDistortion.animations 
{
	import caurina.transitions.Tweener;
	import com.saint.bitmapDistortion.IStretchyImage;
	import com.saint.bitmapDistortion.StretchyImage;
	import com.saint.utils.Logger;
	import flash.geom.Point;
	
	
	
	/**
	 * animates a stretchy image as if it is sucked into the bottom of the screen in the middle
	 * 
	 * 	made for images of 2x2 tiles
	 * 
	 * 
	 * @author kembera
	 */
	public class TwistTopFirst implements IStretchyAnimation
	{
		private var _image:IStretchyImage;
		private var animationPoints:Array = new Array();
		
		//private var transition:String = "easeInQuint"
		private var transition:String = "linear"
		
		private var animationDuration:Number =0.4;
		
		
		/**
		 * CONTRUCTOR
		 * 
		 * @param	image
		 */
		public function TwistTopFirst(image:IStretchyImage) 
		{
			Logger.info("TwistTopFirst.TwistTopFirst");	
			_image = image;
			animationPoints = getAnimationPoints();
		}
		
		/**
		 * check the image has the correct number of tiles
		 */
		private function checkImageFormat():void
		{
			Logger.info("SuckBottomMddle.checkImageFormat");	
		}
		
		/**
		 * GETS THE POINTS
		 */
		private function getAnimationPoints():Array
		{
			Logger.info("TwistTopFirst.getAnimationPoints");
			
			/* check the image has the correct rows and colums */
			var rows:uint = _image.rows;
			var cols:uint = _image.cols;			
					
			Logger.debug("TwistTopFirst.getAnimationPoints rows: " + rows + ", cosl: " + cols )
			
			var points:Array = _image.buttons;
			return points;
			
		}
		public function startAnimation():void
		{
			Logger.info("TwistTopFirst.startAnimation");
			
			var topLeftIndex:Number = 0;
			var topRightIndex:Number = _image.cols;			
			
			var bottomLeftIndex:Number = (_image.cols +1) * (_image.rows ) ;
			trace("  cols: " +  _image.cols);
			trace("  rows: " +  _image.rows);
			
			var bottomRightIndex:Number = (_image.cols +1) * (_image.rows +1) -1;
			
			trace("bottomLeftIndex: " + bottomLeftIndex) ;
			trace("bottomRightIndex: " + bottomRightIndex) ;
			
			var topLeftX:Number = animationPoints[topLeftIndex].x;
			var topRightX:Number = animationPoints[topRightIndex].x;
			
			var bottomLeftX:Number = animationPoints[bottomLeftIndex].x;
			var bottomRightX:Number = animationPoints[bottomRightIndex].x;
									
			Tweener.addTween(animationPoints[topLeftIndex], { time:animationDuration, x:topRightX, transition:transition } );
			Tweener.addTween(animationPoints[topRightIndex], { time:animationDuration, x:topLeftX, transition:transition } );
												
			Tweener.addTween(animationPoints[bottomLeftIndex], { time:animationDuration, x:bottomRightX, transition:transition , delay:animationDuration*2} );
			Tweener.addTween(animationPoints[bottomRightIndex], { time:animationDuration, x:bottomLeftX, transition:transition, delay: animationDuration*2} );
			
		}
		
		
	}

}